package sanrenxing.right.servlet;

import com.google.gson.Gson;
import sanrenxing.right.dao.RoleDao;
import sanrenxing.right.entity.RoleEntity;
import sanrenxing.right.entity.RoleTreeEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

@WebServlet("/RoleServlet")
public class RoleServlet extends HttpServlet {
    //创建角色业务成对象
    RoleDao roleDao=new RoleDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收来自客户端的方法名
        String method=request.getParameter("method");
        //判断未加载角色表格方法--查询数据库中所有角色
        if(method.equals("selectRole")) {
            selectRole(request, response);
        }else if(method.equals("editRole")){//显示树方法
            editRole(request,response);
        }else if(method.equals("insertRoleRight")) {//增加角色方法
            insertRoleRight(request, response);
        }else if (method.equals("updateRoleRight")){//修改角色方法
            this.updateRoleRight(request,response);
        }else if (method.equals("addRole")){
            this.addRole(request,response);
        }
    }

    /**
     * 添加角色
     */
    private void addRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //创建输出对象
        PrintWriter out=resp.getWriter();
        //接收来自客户端的角色名称
        String roleName=req.getParameter("roleName");


        //调用数据层方法，添加角色
        boolean boo=roleDao.addRole(roleName);
        //发送数据
        if (boo){
            out.print("ok");
        }else{
            out.print("no");
        }
    }

    /**
     * 修改角色对应的权限
     */
    private void updateRoleRight(HttpServletRequest req, HttpServletResponse resp) {
        //接收来自客户端的角色编号
        int roleId=Integer.parseInt(req.getParameter("roleId"));
        //接收来自客户端的用户所有权限编号
        String parmList=req.getParameter("parmList");
        //调用删除角色对应全部权限的方法
        roleDao.deleteRoleRights(roleId);
        //分割权限字符串组，将每个权限保存到数组中
        String[] strs=parmList.split(",");
        //foreach遍历权限编号
        for (String str:strs) {
            //调用修改权限的方法，传参权限编号
            roleDao.updateRoleRight(roleId,str);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


    /**
     * 查询的数据库中所有角色方法
     * @param request 请求对象
     * @param response 响应对象
     * @throws ServletException
     * @throws IOException
     */
    protected void selectRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应字符集，防止出现乱码
        response.setCharacterEncoding("utf-8");
        //通过业务层对象调用查询所有角色方法，返回角色集合
        List<RoleEntity> roleList = roleDao.selectRole();
        //创建map集合，以键值对的方式保存layui-table显示表格所需要的数据
        HashMap roleMap=new HashMap();
        roleMap.put("code",0);
        roleMap.put("msg","");
        roleMap.put("count",10);
        roleMap.put("data",roleList);
        //创建Gson对象，为序列化map集合做准备
        Gson gson= new Gson();
        //将map集合类型序列化为json数据类型
        String strJson = gson.toJson(roleMap);
        //创建数据对象，用于响应客户端
        PrintWriter out = response.getWriter();
        //发送数据
        out.println(strJson);
        //清空流管道
        out.flush();
        //关闭流
        out.close();
    }
    /**
     * 查询的服务类方法
     * @param request 请求对象
     * @param response 响应对象
     * @throws ServletException
     * @throws IOException
     */
    protected void editRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收来自客户端发送的角色编号
        int roleId= Integer.parseInt(request.getParameter("roleId"));
        //通过业务对象调用查找树方法返回数集合
        List<RoleTreeEntity> roleTreeList = roleDao.selectRoleRight(roleId);
        //创建Gson对象，为序列化集合做准备
        Gson gson= new Gson();
        //将集合类型序列化为json数据类型
        String strJson = gson.toJson(roleTreeList);
        //设置响应字符集，防止出现乱码
        response.setCharacterEncoding("utf-8");
        //创建数据对象，用于响应客户端
        PrintWriter out = response.getWriter();
        //发送数据
        out.println(strJson);
        //清空流管道
        out.flush();
        //关闭流
        out.close();
    }
    /**
     * 保存权限
     * @param request 请求对象
     * @param response 响应对象
     * @throws ServletException
     * @throws IOException
     */
    protected void insertRoleRight(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strRoleId = request.getParameter("roleId");
        int roleId=0;
        if(strRoleId!=null && !strRoleId.equals(""))
        {
             roleId=Integer.parseInt(strRoleId);
        }
        String strIds = request.getParameter("strIds");
        String[] ids = strIds.split(",");
        //先根据角色ID 删除所有的功能
        boolean flag=roleDao.deleteRoleRight(roleId);
        if(flag){
            //循环添加权限功能
            for (String id : ids) {
                int funId=0;
                if(id!=null && !id.equals(""))
                {
                    funId=Integer.parseInt(id);
                }
                roleDao.insertRoleRight(funId,roleId);
            }
        }

    }
}
